package cn.tedu.notice.controller;

import cn.tedu.notice.pojo.dto.NoticeAddParam;
import cn.tedu.notice.pojo.dto.NoticeQuery;
import cn.tedu.notice.pojo.dto.NoticeUpdateParam;
import cn.tedu.notice.pojo.entity.Notice;
import cn.tedu.notice.pojo.vo.NoticeVO;
import cn.tedu.notice.pojo.vo.PageResult;
import cn.tedu.notice.utils.JsonResult;
import cn.tedu.notice.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("v1/notice/")
@Api(tags = "公告接口")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 创建公告
     *
     * @param noticeAddParam
     * @return
     */
    @ApiOperation("创建公告")
    @PostMapping("save")
    public JsonResult createNotice(@RequestBody NoticeAddParam noticeAddParam) {
        log.debug("新增公告 service入参：noticeAddParam={}", noticeAddParam);
        noticeService.insertNotice(noticeAddParam);
        log.debug("新增公告 service出参：noticeAddParam={}", noticeAddParam);
        return JsonResult.ok();
    }

    @ApiOperation("公告保存到草稿箱")
    @PostMapping("toDraft")
    public JsonResult createToDraft(@RequestBody NoticeUpdateParam noticeUpdateParam) {
        log.debug("保存公告到草稿箱 noticeAddParam={}", noticeUpdateParam);
        noticeService.updateToDraftNotice(noticeUpdateParam);
        return JsonResult.ok();
    }

    @ApiOperation("查找所有公告")
    @GetMapping("select")
    public JsonResult selectAllNotice(NoticeQuery noticeQuery) {
        log.debug("查找公告:noticeQuery={}", noticeQuery);
        List<NoticeVO> noticeList = noticeService.selectNotice(noticeQuery);
        return JsonResult.ok(noticeList);
    }

    @ApiOperation("查看公告详情")
    @GetMapping("selectInfo/{noticeId}")
    public JsonResult selectNoticeById(@PathVariable Integer noticeId) throws Exception {
        log.debug("查看公告详情 noticeId={}", noticeId);
        NoticeVO noticeVO = noticeService.selectNoticeById(noticeId);
        return JsonResult.ok(noticeVO);
    }

    @ApiOperation("删除公告")
    @PostMapping("delete/{id}")
    public JsonResult deleteNoticeById(@PathVariable Integer id) {
        log.debug("删除公告 id={}", id);
        noticeService.deleteNoticeById(id);
        return JsonResult.ok();
    }

    @ApiOperation("更新公告")
    @PostMapping("update")
    public JsonResult updateNoticeById(@RequestBody NoticeUpdateParam noticeUpdateParam) {
        log.debug("更新公告 noticeAddParam={}", noticeUpdateParam);
        noticeService.updateNotice(noticeUpdateParam);
        return JsonResult.ok();
    }

    @ApiModelProperty(value = "根据条件搜索并分页")
    @GetMapping("page")
    public PageResult<NoticeVO> pageQuery(NoticeQuery noticeQuery) {
        log.debug("查询 并分页，查询条件={}",noticeQuery);
        return noticeService.pageQuery(noticeQuery);
    }

    @ApiModelProperty(value = "更新公告状态")
    @PostMapping("changeStatus/{id}")
    public JsonResult changeStatus(@PathVariable Integer id){
        log.debug("更改公告状态 ={}",id);
        noticeService.updateNoticeStatus(id);
        return JsonResult.ok();
    }

}
